package cn.wolfcode.luowowo.website.web.controller;


import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.StrategyQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IUserinfoRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.Userinfo;
import cn.wolfcode.luowowo.search.qo.StrategySearchQo;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateService;
import cn.wolfcode.luowowo.search.vo.SearchStaticsVo;
import cn.wolfcode.luowowo.website.util.UserInfoFromSpring;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/strategy")
@Controller
public class StrategyController {
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService tagService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IUserinfoRedisService userinfoRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateService strategyTemplateService;
    @RequestMapping("/detail")
    public String detail(Long id, Model model) {
        StrategyContent content = detailService.getContentById(id);
        StrategyDetail strategyDetail = detailService.get(id);
        strategyDetail.setStrategyContent(content);
        model.addAttribute("detail",strategyDetail);
        userinfoRedisService.addViewNum(id,1);
        StrategyStatisVO vo=userinfoRedisService.getStrategyStaticVo(id);
        model.addAttribute("vo",vo);
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        //dest
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //tags
        List<StrategyTag> tags=tagService.getTagsByDestId(qo.getDestId());
        model.addAttribute("tags",tags);
        //pageInfo
        PageInfo<Strategy> pageInfo= detailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategy/list";
    }

    @ResponseBody
    @RequestMapping("/commentAdd")
    public AjaxResult commentAdd(StrategyComment strategyComment, @UserInfoFromSpring Userinfo userinfo) {
        strategyComment.setUserId(userinfo.getId());
        strategyComment.setUsername(userinfo.getNickname());
        strategyComment.setCity(userinfo.getCity());
        strategyComment.setLevel(userinfo.getLevel());
        strategyComment.setHeadUrl(userinfo.getHeadImgUrl());
        strategyComment.setCreateTime(new Date());

        strategyCommentService.saveOrUpdate(strategyComment);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/comment")
    public String comment(StrategyCommentQuery qo,Model model,@UserInfoFromSpring Userinfo userinfo) {
        //page对象
        Page<StrategyComment> page=strategyCommentService.query(qo);
        model.addAttribute("page",page);
        model.addAttribute("userInfo",userinfo);
        return "strategy/commentTpl";
    }

    /**
     *
     * @param toid  评论的id
     * @param fromid  用户的id
     */
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        strategyCommentService.thumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("")
    public String list(Model model) {
        //abroadCds
        List<StrategyStatisVO> abroadCds=userinfoRedisService.getAbroadCds();
        model.addAttribute("abroadCds",abroadCds);
//        unabroadCds
        List<StrategyStatisVO> unabroadCds=userinfoRedisService.getUnabroadCds();
        model.addAttribute("unabroadCds",unabroadCds);
//        hotCds
        List<StrategyStatisVO> hotCds=userinfoRedisService.getHotCds();
        model.addAttribute("hotCds",hotCds);
        //commends
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends",commends);
        List<Map<String,Object>> themeList=strategyTemplateService.getTop10ThemeList();
        model.addAttribute("themeCds",themeList);

        //chinas
        List<SearchStaticsVo> chinas=strategyTemplateService.getListByEs("provinceId","provinceName");
        model.addAttribute("chinas",chinas);
        //abroads
        List<SearchStaticsVo> abroads=strategyTemplateService.getListByEs("countryId","countryName");
        for (SearchStaticsVo abroad : abroads) {
            if(abroad.getName().equals("中国")) {
                abroads.remove(abroad);
                break;
            }
        }
        model.addAttribute("abroads",abroads);
        //themes
        List<SearchStaticsVo> themes=strategyTemplateService.getListByEs("themeId","themeName");
        model.addAttribute("themes",themes);
        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchForm( @ModelAttribute("qo")StrategySearchQo qo,Model model) {
        //page
        Page page=strategyTemplateService.queryByDifferentType(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

}
